<?php
/**
 * Created by PhpStorm.
 * User: wxy
 * Date: 2017/8/30
 * Time: 15:31
 */

namespace island\controllers;


use island\models\ClassModel;

use island\models\ClassStu;
use island\models\TSpu;
use Yii;

use yii\base\Exception;
use yii\web\Controller;


class TestController extends Controller
{
    public function actionIndex()
    {
        var_dump(113);
        die;
    }

    public function actionList()
    {

        $type = Yii::$app->request->get('type');

        if ($type == 1) {
            $data = [
                ['name' => '买入', 'num' => '+200个', 'time' => '2019-2-13 12:12', 'all' => '359'],
                ['name' => '卖出', 'num' => '-200个', 'time' => '2019-2-13 12:12', 'all' => '359'],
                ['name' => '买入', 'num' => '+200个', 'time' => '2019-2-13 12:12', 'all' => '359'],
            ];
        } elseif ($type == 2) {
            $data = [
                ['name' => '邀请好友', 'num' => '+20元', 'time' => '2019-2-13 12:12', 'all' => '500元'],
                ['name' => '邀请好友', 'num' => '+20元', 'time' => '2019-2-13 12:12', 'all' => '500元'],
                ['name' => '邀请好友', 'num' => '+20元', 'time' => '2019-2-13 12:12', 'all' => '500元'],
                ['name' => '邀请好友', 'num' => '+20元', 'time' => '2019-2-13 12:12', 'all' => '500元'],
            ];
        } elseif ($type == 3) {
            $data = [
                ['name' => '提现', 'num' => '-100元', 'time' => '2019-2-13 12:12', 'all' => '500元'],
                ['name' => '提现', 'num' => '-100元', 'time' => '2019-2-13 12:12', 'all' => '500元'],
                ['name' => '提现', 'num' => '-100元', 'time' => '2019-2-13 12:12', 'all' => '500元'],
                ['name' => '提现', 'num' => '-100元', 'time' => '2019-2-13 12:12', 'all' => '500元'],
            ];
        } else {
            $data = [];
        }
        return json_encode([
            'status' => 200,
            'data' => $data
        ]);
    }


    /**
     * @throws \yii\db\Exception
     */
    public function actionA()
    {


        $transaction = Yii::$app->db->beginTransaction();
        try {

            $model = (new ClassModel());
            $model->class_name = 'a';
            $res1 = $model->save();
            var_dump($model->class_id);
            throw new Exception('aa');


            $transaction->commit();
        } catch (Exception $e) {
            $transaction->rollBack();
            var_dump('rollBack');
        }

    }


    /**
     * @throws \yii\db\Exception
     */
    public function actionB()
    {


        $transaction = Yii::$app->db->beginTransaction();
        try {

            $model = (new ClassModel());
            $model->class_name = 'a';
            $res1 = $model->save();
            var_dump($model->class_id);

            //
            $model2 = (new ClassStu());
            $model2->stu_name = 'hehe';
            $model2->class_id = $model->primaryKey;

            var_dump($model2->save());
            throw new Exception('aa');


            $transaction->commit();
        } catch (Exception $e) {
            $transaction->rollBack();
            var_dump('rollBack');
        }

    }


    public function actionC()
    {

        $page = Yii::$app->request->get('page', 1);

        //$data = TSpu::find()->select('t_spu.SpuId as s,t_spu.OnDate,t_spu_shop.BranchId')->leftJoin('t_spu_shop','t_spu.SpuId = t_spu_shop.SpuId')->orderBy(' OnDate desc ');
        //$data = TSpu::find()->select('t_spu.SpuId,t_spu.OnDate,t_spu_shop.BranchId,t_spu_shop.SpuShopId ')->leftJoin('t_spu_shop','t_spu.SpuId = t_spu_shop.SpuId')->orderBy(' OnDate desc ')
        //->indexBy('SpuShopId');

        //$data = TSpu::find()->select('t_spu.OnDate,t_spu_shop.BranchId')->leftJoin('t_spu_shop','t_spu.SpuId = t_spu_shop.SpuId')->orderBy(' OnDate desc ');


        $res['goodslist'] = $data->offset(($page - 1) * 20)->limit(20)->asArray()->all();
        $res['count'] = count($res['goodslist']);
        return json_encode($res);
    }


















}